<?php

namespace app\admin\model\usedcar;

use think\Model;


class Car extends Model
{

    

    

    // 表名
    protected $name = 'used_car';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'image_text',
        'brand_name_text'
    ];

    public function brand()
    {
        return $this->belongsTo('app\admin\model\used\car\Brand', 'car_brand_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function userlci()
    {
        return $this->belongsTo('app\common\model\car\CarUserLciModel', 'used_car_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    // 定义获取器，将时间戳转换为时间格式
    public function getCreateTimeAttr($value,$data)
    {
        if(isset($data['createtime'])){
            $value = date('Y-m-d H:i:s',$value);
        }
        return $value;
    }

    public function getImageTextAttr($value,$data)
    {
        if(isset($data['image'])){
            $value = cdnurl($data['image'],true);
        }
        return $value;
    }

    public function getBrandNameTextAttr($value,$data)
    {
        if(isset($data['car_brand_id'])){
            $value = Brand::where(['id'=>$data['car_brand_id']])->value('barname');
        }
        return $value;
    }

}
